package cn.tedu.boyou.website.controller;

import cn.tedu.boyou.acticle.domain.Destination;
import cn.tedu.boyou.acticle.domain.Sight;
import cn.tedu.boyou.acticle.domain.StrategyDetail;
import cn.tedu.boyou.acticle.domain.Travel;
import cn.tedu.boyou.acticle.service.IDestinationService;
import cn.tedu.boyou.acticle.service.ISightService;
import cn.tedu.boyou.acticle.service.IStrategyDetailService;
import cn.tedu.boyou.acticle.service.ITravelService;
import cn.tedu.boyou.acticle.vo.TravelStatisVO;
import cn.tedu.boyou.cache.prefix.TravelStatisKeyPrefix;
import cn.tedu.boyou.cache.prefix.UserInfoKeyPrefix;
import cn.tedu.boyou.cache.service.ITravelStatisRedisService;
import cn.tedu.boyou.cache.service.IUserInfoRedisService;
import cn.tedu.boyou.comment.domain.SightReview;
import cn.tedu.boyou.comment.service.ISightReviewService;
import cn.tedu.boyou.common.query.TravelQuery;
import cn.tedu.boyou.common.util.AjaxResult;
import cn.tedu.boyou.member.domain.UserInfo;
import cn.tedu.boyou.member.service.IUserInfoService;
import cn.tedu.boyou.member.vo.FansStaticVo;
import cn.tedu.boyou.website.annotation.RequireLogin;
import cn.tedu.boyou.website.annotation.UserParam;
import cn.tedu.boyou.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/mine")
public class MineController {

    @Reference
    private ITravelService travelService;

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ISightReviewService sightReviewService;

    @Reference
    private ISightService sightService;

    /**
     * 我的窝
     *
     * @param model
     * @param userInfo
     * @return
     */
    @RequestMapping("/homepage")
    @RequireLogin
    public String home(Model model, @UserParam UserInfo userInfo) {
        //共享登录用户
        model.addAttribute("userInfo", userInfo);

        //查询最新游记
        List<Travel> travels = travelService.getNewTravel(userInfo.getId());

        //查询用户游记数量
        int count = travelService.getTravelConut(userInfo.getId());

        /**
         * 查看关注id
         */
        List<Integer> concernsId = userInfoRedisService.getConcernsId(UserInfoKeyPrefix.USER_INFO_FANS, userInfo.getId());

        List<FansStaticVo> vos = new ArrayList<>();

        if (concernsId != null) {
            //查询出每个关注用户的信息
            for (Integer cid : concernsId) {
                Long aLong = Long.valueOf(cid);
                FansStaticVo vo = new FansStaticVo();
                UserInfo info = userInfoService.get(aLong);
                vo.setId(aLong);
                vo.setNickname(info.getNickname());
                vo.setHeadImgUrl(info.getHeadImgUrl());
                vos.add(vo);

            }
        }
        model.addAttribute("vos", vos);
        model.addAttribute("count", count);


        //遍历用户的所有游记
        for (Travel travel : travels) {
            //从redis里获取获取点赞数
            TravelStatisVO travelVo = travelStatisRedisService.getTravelVo(travel.getId());

            //获取游记目的地的父目的地的对象("上海"---->"中国")
            Destination dest = travel.getDest();
            Long destId = dest.getId();
            Destination hasParent = destinationService.getParentByName(destId);

            //将对象含有父目的对象的传给travel
            travel.setDest(hasParent);

            if (travelVo != null) {
                //将从redis获取的点赞数设置到travel对象里
                travel.setThumbsupnum(travelVo.getThumbsupnum());
            }
        }
        model.addAttribute("travels", travels);

        //查询点评
        List<SightReview> sightReviews = sightReviewService.selectByUserId(userInfo.getId());

        for (SightReview sightReview : sightReviews) {
            Long sightId = sightReview.getSightId();
            Sight sight = sightService.selectBySightId(sightId);
            String sightName = sight.getName();
            sightReview.setTravelTitle(sightName);
        }
        int size = sightReviews.size();

        model.addAttribute("size",size);
        model.addAttribute("sightReviews",sightReviews);


        return "/mine/homepage";
    }


    /**
     * 我的游记
     *
     * @param model
     * @param qo
     * @param userInfo
     * @return
     */
    @RequestMapping("/mytravelnotes")
    @RequireLogin
    public Object mytravelnotes(Model model, TravelQuery qo, @UserParam UserInfo userInfo) {
        //查询该用户的游记
        List<Travel> travels = travelService.listByUserId(userInfo.getId());
        //总游记数
        int count = travels.size();
        //总回复数
        Long replays = 0L;
        //总阅读数
        Long views = 0L;

        for (Travel travel : travels) {
            views += travel.getViewnum();
            replays += travel.getReplynum();
        }
        //用户信息
        model.addAttribute("userInfo", userInfo);
        //总游记数量
        model.addAttribute("count", count);
        //分页查询我的全部游记
        PageInfo pageInfo = travelService.queryByUserId(qo, userInfo.getId());

        //获取用户的所有游记
        List<Travel> travelList = pageInfo.getList();

        //迭代游记
        for (Travel travel : travelList) {

            //获取游记目的地的父目的地的对象("上海"---->"中国")
            Destination dest = travel.getDest();
            Long destId = dest.getId();
            Destination hasParent = destinationService.getParentByName(destId);

            //将对象含有父目的对象的传给travel
            travel.setDest(hasParent);

            //获取redis的vo数据
            TravelStatisVO travelVo = travelStatisRedisService.getTravelVo(travel.getId());
            if (travelVo != null) {
                //将从redis获取的点赞数设置到travel对象里
                travel.setThumbsupnum(travelVo.getThumbsupnum());
            }
        }
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("replays", replays);
        model.addAttribute("views", views);


        return new AjaxResult();
    }


    /**
     * 个人设置
     *
     * @param model
     * @param userInfo
     * @return
     */
    @RequestMapping("/setting")
    @RequireLogin
    public Object setting(Model model, @UserParam UserInfo userInfo) {
        //共享登录用户
        model.addAttribute("userInfo", userInfo);


        return new AjaxResult();
    }

    /**
     * 关注用户
     *
     * @param model
     * @param userInfo 登录用户
     * @param OtherId  被关注的用户id
     * @return
     */
    @RequestMapping("/addFans")
    @RequireLogin
    public Object addFans(Model model, @UserParam UserInfo userInfo, Long OtherId) {

        //添加关注用户
        userInfoRedisService.addFans(UserInfoKeyPrefix.USER_INFO_FANS, userInfo.getId(), OtherId);

        //共享登录用户
        model.addAttribute("userInfo", userInfo);

        return new AjaxResult();
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) {
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }

    /**
     * 修改个人信息
     *
     * @param userInfo 登录用户
     * @return
     */
    @RequestMapping("/input")
    @RequireLogin
    @ResponseBody
    public Object input(UserInfo userInfo) {
        userInfoService.update(userInfo);
        return new AjaxResult();
    }

    /**
     * 游记点赞
     *
     * @param model
     * @param travelId
     * @param userInfo
     * @return
     */
    @RequestMapping("/travelThumbup")
    @ResponseBody
    public Object travelThumbup(Model model, Long travelId, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            return new AjaxResult("请先登录", AjaxResult.CODE_TYPE_UNLOGIN);
        }
        boolean ret = travelStatisRedisService.thumbup(TravelStatisKeyPrefix.TRAVEL_STATIS_THUMBSUP, travelId, userInfo.getId());
        TravelStatisVO travelVo = travelStatisRedisService.getTravelVo(travelId);
        int thumbsupnum = travelVo.getThumbsupnum();
        return new AjaxResult(ret, "").addData(thumbsupnum);
    }


    /**
     * 用户收藏数据
     *
     * @param userInfo 登录用户
     * @return
     */
    @RequestMapping("/travelcollection")
    @RequireLogin
    public String travelcollection(Model model, @UserParam UserInfo userInfo) {

        List<Travel> travels = new ArrayList<>();

        List<StrategyDetail> strategies = new ArrayList<>();

        List<Destination> destinations = new ArrayList<>();

        //查询redis的游记收藏
        Set<String> travelsId = userInfoRedisService.getTravelcollectionById(userInfo.getId());
        //查询redis的攻略收藏
        Set<String> strategiesId = userInfoRedisService.getStrategycollectionById(userInfo.getId());
        //查询地点收藏
        Set<String> destinationsId = userInfoRedisService.getDestById(userInfo.getId());

        if (travelsId != null) {
            for (String tid : travelsId) {
                Long aLong = Long.valueOf(tid);
                Travel travel = travelService.get(aLong);
                travels.add(travel);
            }
        }
        if (strategiesId != null) {
            for (String sid : strategiesId) {
                Long aLong = Long.valueOf(sid);
                StrategyDetail strategy = strategyDetailService.get(aLong);
                strategies.add(strategy);
            }
        }
        if (destinationsId != null) {
            for (String did : destinationsId) {
                Long aLong = Long.valueOf(did);
                Destination dest = destinationService.get(aLong);
                destinations.add(dest);
            }
        }
        model.addAttribute("travels", travels);
        model.addAttribute("strategies", strategies);
        model.addAttribute("destinations", destinations);

        return "mine/travelcollection";
    }


    /**
     * 我的点评
     *
     * @param model
     * @param userInfo
     * @return
     */
    @RequestMapping("/review")
    @RequireLogin
    public Object review(Model model, @UserParam UserInfo userInfo) {
        //共享登录用户
        model.addAttribute("userInfo", userInfo);

        List<SightReview> sightReviews = sightReviewService.selectByUserId(userInfo.getId());

        for (SightReview sightReview : sightReviews) {
            Long sightId = sightReview.getSightId();
            Sight sight = sightService.selectBySightId(sightId);
            String sightName = sight.getName();
            sightReview.setTravelTitle(sightName);
        }
        int size = sightReviews.size();

        model.addAttribute("size",size);
        model.addAttribute("sightReviews",sightReviews);

        return new AjaxResult();
    }


}
